package com.ahzy.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.appcompat.widget.AppCompatImageView;
import com.qmuiteam.qmui.R;

/* loaded from: classes3.dex */
public class QMUIRadiusImageView extends AppCompatImageView {
    public static final Bitmap.Config J = Bitmap.Config.ARGB_8888;
    public ColorFilter A;
    public ColorFilter B;
    public BitmapShader C;
    public boolean D;
    public final RectF E;
    public Bitmap F;
    public final Matrix G;
    public int H;
    public int I;

    /* renamed from: n */
    public boolean f1669n;

    /* renamed from: o */
    public boolean f1670o;

    /* renamed from: p */
    public boolean f1671p;

    /* renamed from: q */
    public int f1672q;

    /* renamed from: r */
    public int f1673r;

    /* renamed from: s */
    public int f1674s;

    /* renamed from: t */
    public int f1675t;

    /* renamed from: u */
    public int f1676u;

    /* renamed from: v */
    public boolean f1677v;

    /* renamed from: w */
    public int f1678w;

    /* renamed from: x */
    public int f1679x;

    /* renamed from: y */
    public Paint f1680y;

    /* renamed from: z */
    public final Paint f1681z;

    public QMUIRadiusImageView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, R.attr.QMUIRadiusImageViewStyle);
    }

    public QMUIRadiusImageView(Context context, AttributeSet attributeSet, int i7) {
        super(context, attributeSet, i7);
        this.f1669n = false;
        this.f1670o = false;
        this.f1671p = false;
        this.f1677v = true;
        this.f1679x = 4369;
        this.D = false;
        this.E = new RectF();
        Paint paint = new Paint();
        this.f1681z = paint;
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        this.G = new Matrix();
        setScaleType(ImageView.ScaleType.CENTER_CROP);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.QMUIRadiusImageView, i7, 0);
        this.f1672q = obtainStyledAttributes.getDimensionPixelSize(R.styleable.QMUIRadiusImageView_qmui_border_width, 0);
        this.f1673r = obtainStyledAttributes.getColor(R.styleable.QMUIRadiusImageView_qmui_border_color, -7829368);
        this.f1674s = obtainStyledAttributes.getDimensionPixelSize(R.styleable.QMUIRadiusImageView_qmui_selected_border_width, this.f1672q);
        this.f1675t = obtainStyledAttributes.getColor(R.styleable.QMUIRadiusImageView_qmui_selected_border_color, this.f1673r);
        int color = obtainStyledAttributes.getColor(R.styleable.QMUIRadiusImageView_qmui_selected_mask_color, 0);
        this.f1676u = color;
        if (color != 0) {
            this.B = new PorterDuffColorFilter(this.f1676u, PorterDuff.Mode.DARKEN);
        }
        this.f1677v = obtainStyledAttributes.getBoolean(R.styleable.QMUIRadiusImageView_qmui_is_touch_select_mode_enabled, true);
        boolean z8 = obtainStyledAttributes.getBoolean(R.styleable.QMUIRadiusImageView_qmui_is_circle, false);
        this.f1671p = z8;
        if (!z8) {
            this.f1670o = obtainStyledAttributes.getBoolean(R.styleable.QMUIRadiusImageView_qmui_is_oval, false);
        }
        if (!this.f1670o) {
            this.f1678w = obtainStyledAttributes.getDimensionPixelSize(R.styleable.QMUIRadiusImageView_qmui_corner_radius, 0);
            this.f1679x = 4369;//obtainStyledAttributes.getInteger(com.ahzy.base.R.styleable.QMUIRadiusImageView_qmui_radius_corner, 4369);
        }
        obtainStyledAttributes.recycle();
    }

    private Bitmap getBitmap() {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return null;
        }
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
        try {
            boolean z8 = drawable instanceof ColorDrawable;
            Bitmap.Config config = J;
            Bitmap createBitmap = z8 ? Bitmap.createBitmap(2, 2, config) : Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), config);
            Canvas canvas = new Canvas(createBitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return createBitmap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public final void b() {
        if (getBitmap() == this.F) {
            return;
        }
        Bitmap bitmap = getBitmap();
        this.F = bitmap;
        if (bitmap == null) {
            this.C = null;
        } else {
            this.D = true;
            Bitmap bitmap2 = this.F;
            Shader.TileMode tileMode = Shader.TileMode.CLAMP;
            this.C = new BitmapShader(bitmap2, tileMode, tileMode);
            if (this.f1680y == null) {
                Paint paint = new Paint();
                this.f1680y = paint;
                paint.setAntiAlias(true);
            }
            this.f1680y.setShader(this.C);
            requestLayout();
        }
        invalidate();
    }

    public int getBorderColor() {
        return this.f1673r;
    }

    public int getBorderWidth() {
        return this.f1672q;
    }

    public int getCornerRadius() {
        return this.f1678w;
    }

    public int getSelectedBorderColor() {
        return this.f1675t;
    }

    public int getSelectedBorderWidth() {
        return this.f1674s;
    }

    public int getSelectedMaskColor() {
        return this.f1676u;
    }

    @Override // android.view.View
    public final boolean isSelected() {
        return this.f1669n;
    }

    @Override // android.widget.ImageView, android.view.View
    public final void onDraw(Canvas canvas) {
        Bitmap bitmap;
        int i7;
        ColorFilter colorFilter;
        int i9;
        int width = getWidth();
        int height = getHeight();
        if (width > 0 && height > 0 && this.F != null && this.C != null) {
            if (this.H != width || this.I != height || this.D) {
                this.H = width;
                this.I = height;
                Matrix matrix = this.G;
                matrix.reset();
                this.D = false;
                if (this.C != null && (bitmap = this.F) != null) {
                    float width2 = bitmap.getWidth();
                    float height2 = this.F.getHeight();
                    float max = Math.max(this.H / width2, this.I / height2);
                    matrix.setScale(max, max);
                    matrix.postTranslate((-((width2 * max) - this.H)) / 2.0f, (-((max * height2) - this.I)) / 2.0f);
                    this.C.setLocalMatrix(matrix);
                    this.f1680y.setShader(this.C);
                }
            }
            Paint paint = this.f1681z;
            if (this.f1669n) {
                i7 = this.f1675t;
            } else {
                i7 = this.f1673r;
            }
            paint.setColor(i7);
            Paint paint2 = this.f1680y;
            if (this.f1669n) {
                colorFilter = this.B;
            } else {
                colorFilter = this.A;
            }
            paint2.setColorFilter(colorFilter);
            if (this.f1669n) {
                i9 = this.f1674s;
            } else {
                i9 = this.f1672q;
            }
            float f = i9;
            paint.setStrokeWidth(f);
            float f9 = (f * 1.0f) / 2.0f;
            if (this.f1671p) {
                float width3 = getWidth() / 2;
                canvas.drawCircle(width3, width3, width3, this.f1680y);
                if (i9 > 0) {
                    canvas.drawCircle(width3, width3, width3 - f9, paint);
                    return;
                }
                return;
            }
            RectF rectF = this.E;
            rectF.left = f9;
            rectF.top = f9;
            float f10 = width - f9;
            rectF.right = f10;
            float f11 = height - f9;
            rectF.bottom = f11;
            if (this.f1670o) {
                canvas.drawOval(rectF, this.f1680y);
                if (i9 > 0) {
                    canvas.drawOval(rectF, paint);
                    return;
                }
                return;
            }
            int i10 = this.f1678w;
            canvas.drawRoundRect(rectF, i10, i10, this.f1680y);
            if ((this.f1679x & 1) == 0) {
                rectF.left = f9;
                rectF.top = f9;
                int i11 = this.f1678w;
                rectF.right = i11 + f9;
                rectF.bottom = i11 + f9;
                canvas.drawRect(rectF, this.f1680y);
            }
            if ((this.f1679x & 256) == 0) {
                rectF.left = f9;
                int i12 = this.f1678w;
                float f12 = f11 - i12;
                rectF.top = f12;
                rectF.right = i12 + f9;
                rectF.bottom = f12 + i12;
                canvas.drawRect(rectF, this.f1680y);
            }
            if ((this.f1679x & 16) == 0) {
                int i13 = this.f1678w;
                float f13 = f10 - i13;
                rectF.left = f13;
                rectF.top = f9;
                rectF.right = f13 + i13;
                rectF.bottom = f9 + i13;
                canvas.drawRect(rectF, this.f1680y);
            }
            if ((this.f1679x & 4096) == 0) {
                int i14 = this.f1678w;
                float f14 = f10 - i14;
                rectF.left = f14;
                float f15 = f11 - i14;
                rectF.top = f15;
                rectF.right = f14 + i14;
                rectF.bottom = f15 + i14;
                canvas.drawRect(rectF, this.f1680y);
            }
            if (i9 > 0) {
                int i15 = this.f1678w;
                if (i15 == 0 || this.f1679x == 4369) {
                    canvas.drawRoundRect(rectF, i15, i15, paint);
                }
            }
        }
    }

    @Override // android.widget.ImageView, android.view.View
    public final void onMeasure(int i7, int i9) {
        super.onMeasure(i7, i9);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        if (this.f1671p) {
            int min = Math.min(measuredWidth, measuredHeight);
            setMeasuredDimension(min, min);
            return;
        }
        int mode = View.MeasureSpec.getMode(i7);
        int mode2 = View.MeasureSpec.getMode(i9);
        Bitmap bitmap = this.F;
        if (bitmap == null) {
            return;
        }
        if (mode == Integer.MIN_VALUE || mode == 0 || mode2 == Integer.MIN_VALUE || mode2 == 0) {
            float width = bitmap.getWidth();
            float height = this.F.getHeight();
            float f = measuredWidth / width;
            float f9 = measuredHeight / height;
            if (f == f9) {
                return;
            }
            if (f < f9) {
                setMeasuredDimension(measuredWidth, (int) (height * f));
            } else {
                setMeasuredDimension((int) (width * f9), measuredHeight);
            }
        }
    }

    @Override // android.view.View
    public final boolean onTouchEvent(MotionEvent motionEvent) {
        if (!isClickable()) {
            setSelected(false);
            return super.onTouchEvent(motionEvent);
        }
        if (!this.f1677v) {
            return super.onTouchEvent(motionEvent);
        }
        int action = motionEvent.getAction();
        if (action == 0) {
            setSelected(true);
        } else if (action == 1 || action == 3 || action == 4 || action == 8) {
            setSelected(false);
        }
        return super.onTouchEvent(motionEvent);
    }

    @Override // android.widget.ImageView
    public void setAdjustViewBounds(boolean z8) {
        if (z8) {
            throw new IllegalArgumentException("不支持adjustViewBounds");
        }
    }

    public void setBorderColor(@ColorInt int i7) {
        if (this.f1673r != i7) {
            this.f1673r = i7;
            invalidate();
        }
    }

    public void setBorderWidth(int i7) {
        if (this.f1672q != i7) {
            this.f1672q = i7;
            invalidate();
        }
    }

    public void setCircle(boolean z8) {
        if (this.f1671p != z8) {
            this.f1671p = z8;
            requestLayout();
            invalidate();
        }
    }

    @Override // android.widget.ImageView
    public void setColorFilter(ColorFilter colorFilter) {
        if (this.A == colorFilter) {
            return;
        }
        this.A = colorFilter;
        if (this.f1669n) {
            return;
        }
        invalidate();
    }

    public void setCornerRadius(int i7) {
        if (this.f1678w != i7) {
            this.f1678w = i7;
            if (this.f1671p || this.f1670o) {
                return;
            }
            invalidate();
        }
    }

    @Override // androidx.appcompat.widget.AppCompatImageView, android.widget.ImageView
    public void setImageBitmap(Bitmap bitmap) {
        super.setImageBitmap(bitmap);
        b();
    }

    @Override // androidx.appcompat.widget.AppCompatImageView, android.widget.ImageView
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        b();
    }

    @Override // androidx.appcompat.widget.AppCompatImageView, android.widget.ImageView
    public void setImageResource(@DrawableRes int i7) {
        super.setImageResource(i7);
        b();
    }

    @Override // androidx.appcompat.widget.AppCompatImageView, android.widget.ImageView
    public void setImageURI(Uri uri) {
        super.setImageURI(uri);
        b();
    }

    public void setOval(boolean z8) {
        boolean z9 = false;
        if (z8 && this.f1671p) {
            this.f1671p = false;
            z9 = true;
        }
        if (this.f1670o != z8 || z9) {
            this.f1670o = z8;
            requestLayout();
            invalidate();
        }
    }

    public void setQmuiRadiusCorner(int i7) {
        this.f1679x = i7;
        invalidate();
    }

    @Override // android.widget.ImageView
    public void setScaleType(ImageView.ScaleType scaleType) {
        if (scaleType != ImageView.ScaleType.CENTER_CROP) {
            throw new IllegalArgumentException(String.format("不支持ScaleType %s", scaleType));
        }
        super.setScaleType(scaleType);
    }

    @Override // android.widget.ImageView, android.view.View
    public void setSelected(boolean z8) {
        if (this.f1669n != z8) {
            this.f1669n = z8;
            invalidate();
        }
    }

    public void setSelectedBorderColor(@ColorInt int i7) {
        if (this.f1675t != i7) {
            this.f1675t = i7;
            if (this.f1669n) {
                invalidate();
            }
        }
    }

    public void setSelectedBorderWidth(int i7) {
        if (this.f1674s != i7) {
            this.f1674s = i7;
            if (this.f1669n) {
                invalidate();
            }
        }
    }

    public void setSelectedColorFilter(ColorFilter colorFilter) {
        if (this.B == colorFilter) {
            return;
        }
        this.B = colorFilter;
        if (this.f1669n) {
            invalidate();
        }
    }

    public void setSelectedMaskColor(@ColorInt int i7) {
        if (this.f1676u != i7) {
            this.f1676u = i7;
            this.B = i7 != 0 ? new PorterDuffColorFilter(this.f1676u, PorterDuff.Mode.DARKEN) : null;
            if (this.f1669n) {
                invalidate();
            }
        }
        this.f1676u = i7;
    }

    public void setTouchSelectModeEnabled(boolean z8) {
        this.f1677v = z8;
    }
}